package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.Clazzs;
import com.xjc.business.entity.domain.CollageMajorRelations;
import com.xjc.business.entity.domain.MajorClazzRelations;
import com.xjc.business.entity.domain.Majors;
import com.xjc.business.entity.po.coladmin.MajorPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.coladmin.ClassVo;
import com.xjc.business.entity.vo.coladmin.MajorVo;
import com.xjc.business.entity.vo.coladmin.MajorWithClass;
import com.xjc.business.service.ClazzsService;
import com.xjc.business.service.CollageMajorRelationsService;
import com.xjc.business.service.MajorClazzRelationsService;
import com.xjc.business.service.MajorsService;
import com.xjc.business.mapper.MajorsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【majors(专业表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class MajorsServiceImpl extends ServiceImpl<MajorsMapper, Majors>
    implements MajorsService {

    @Autowired
    @Lazy
    CollageMajorRelationsService collageMajorRelationsService;

    @Autowired
    @Lazy
    MajorClazzRelationsService majorClazzRelationsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    /**
     * 获取专业分页
     * @param pageSize    每页条数
     * @param currentPage 当前页
     * @param collegeId   学院id
     * @param majorName   专业名称
     * @return 数据
     */
    @Override
    public Result<PageVo<MajorVo>> getMajorPage(Integer pageSize, Integer currentPage, Integer collegeId, String majorName) {
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId, collegeId);
        List<Integer> collectIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

        PageVo<MajorVo> pageVo = new PageVo<>();

        if (collectIdList.isEmpty()) {
            return Result.okWithData(pageVo);
        }

        LambdaQueryWrapper<Majors> majorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorsLambdaQueryWrapper.like(!majorName.isEmpty(),Majors::getName,majorName);
        majorsLambdaQueryWrapper.in(Majors::getId,collectIdList);

        Page<Majors> majorsPage = new Page<>(currentPage, pageSize);
        page(majorsPage, majorsLambdaQueryWrapper);
        List<Majors> majorsPageRecords = majorsPage.getRecords();
        List<MajorVo> majorVoList = majorsPageRecords.stream().map(majors -> {
            MajorVo majorVo = new MajorVo();
            majorVo.setId(majors.getId());
            majorVo.setName(majors.getName());
            majorVo.setCreateTime(majors.getCreateTime());
            return majorVo;
        }).collect(Collectors.toList());

        pageVo.setCurrent(currentPage);
        pageVo.setPageCount(majorsPage.getPages());
        pageVo.setSize(pageSize);
        pageVo.setTotal(majorsPage.getTotal());
        pageVo.setData(majorVoList);

        return Result.okWithData(pageVo);
    }

    /**
     * 新增专业
     * @param majorPo 专业信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> addMajor(MajorPo majorPo) {
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId,majorPo.getCollegeId());
        List<Integer> collectIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());
        if(!collectIdList.isEmpty()){
            LambdaQueryWrapper<Majors> majorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            majorsLambdaQueryWrapper.eq(Majors::getName,majorPo.getName());
            majorsLambdaQueryWrapper.in(Majors::getId,collectIdList);

            if (count(majorsLambdaQueryWrapper) > 0) {
                return Result.ok("专业已经存在");
            }

        }

        Majors majors = new Majors();
        majors.setName(majorPo.getName());
        majors.setCreateTime(new Date());
        majors.setUpdateTime(new Date());
        save(majors);

        CollageMajorRelations collageMajorRelations = new CollageMajorRelations();
        collageMajorRelations.setColId(majorPo.getCollegeId());
        collageMajorRelations.setMajId(majors.getId());
        collageMajorRelationsService.save(collageMajorRelations);
        return Result.okWithData("增加成功");
    }

    /**
     * 获取专业列表
     *
     * @param collegeId 学院id
     * @return 数据
     */
    @Override
    public Result<List<MajorVo>> getMajorList(Integer collegeId) {
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId, collegeId);

        List<Integer> collectIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

        List<MajorVo> majorVoList = new ArrayList<>();

        if(collectIdList.isEmpty()) {
            return Result.okWithData(majorVoList);
        }

        LambdaQueryWrapper<Majors> majorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorsLambdaQueryWrapper.in(Majors::getId, collectIdList);
        List<Majors> majorsList = list(majorsLambdaQueryWrapper);

        for (Majors majors : majorsList) {
            MajorVo majorVo = new MajorVo();
            majorVo.setId(majors.getId());
            majorVo.setName(majors.getName());
            majorVo.setCreateTime(majors.getCreateTime());
            majorVoList.add(majorVo);
        }

        return Result.okWithData(majorVoList);
    }

    /**
     * 获取专业列表（包含二级班级）
     *
     * @param collegeId 学院id
     * @return 数据
     */
    @Override
    public Result<List<MajorWithClass>> getMajorWithClassList(Integer collegeId) {
        LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId, collegeId);
        List<Integer> collectIdList = collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().map(CollageMajorRelations::getMajId).collect(Collectors.toList());

        if(collectIdList.isEmpty()){
            return Result.okWithData(new ArrayList<>());
        }

        LambdaQueryWrapper<Majors> majorsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorsLambdaQueryWrapper.in(Majors::getId, collectIdList);

        List<Majors> majorsList = list(majorsLambdaQueryWrapper);

        List<MajorWithClass> majorWithClassList = majorsList.stream().map(majors -> {
            MajorWithClass majorWithClass = new MajorWithClass();
            majorWithClass.setMajorId(majors.getId());
            majorWithClass.setMajorName(majors.getName());

            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId, majors.getId());
            List<Integer> collectIdList2 = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());
            if(collectIdList2.isEmpty()){
                majorWithClass.setClassVoList(new ArrayList<>());
            }else{
                LambdaQueryWrapper<Clazzs> clazzsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                clazzsLambdaQueryWrapper.in(Clazzs::getId, collectIdList2);
                List<Clazzs> clazzsList = clazzsService.list(clazzsLambdaQueryWrapper);
                List<ClassVo> classVoList = clazzsList.stream().map(clazzs -> {
                    ClassVo classVo = new ClassVo();
                    classVo.setId(clazzs.getId());
                    classVo.setNum(clazzs.getNum());
                    classVo.setCreateTime(clazzs.getCreateTime());
                    return classVo;
                }).collect(Collectors.toList());
                majorWithClass.setClassVoList(classVoList);
            }
            return majorWithClass;
        }).collect(Collectors.toList());

        return Result.okWithData(majorWithClassList);
    }
}